
//Simple Client

import java.io.*;
import java.net.*;

public class SimpleClient {

    public static void main(String arg[]){
	OutputStream	writer = null;
	InputStream	reader = null;
	byte		buf [] = new byte[200];
	Socket		sk = null;

	if ( arg.length < 1 ) {
		System.out.println("Usage: java SimpleClient userID");
		System.exit(0);
	}

	try {
	    sk = new Socket("sleipnir.cs.csubak.edu", 9912);
	    writer=  sk.getOutputStream() ;
	    reader = sk.getInputStream () ;
	
	    Thread t = new Thread ( new ClientAgent( reader) );
	    t.start();
	    int k;
	    while ( true ) {
		k = System.in.read( buf, 0, buf.length );
		writer.write((arg[0] + ": " + (new String(buf, 0, k)).trim()).getBytes() );
	    	writer.flush();

	    }
	} catch (Exception e) { e.printStackTrace(); }
     }
}

class ClientAgent implements Runnable {
   InputStream reader = null;
   int		k = 0;
   byte		buf[] = new byte [500];
   public ClientAgent( InputStream in ) {
	reader = in; 
   }


   public void run() {
     try {
	while ( true ) {
		k = reader.read(buf, 0, buf.length );
	 	System.out.println((new String(buf, 0, k)).trim());

	}
     } catch (Exception e) { e.printStackTrace(); }
   }
}
